package ae.pcfc.etks.online.itworx.ehs.web.util;

import java.io.File;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.validator.GenericValidator;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import ae.pcfc.etks.online.itworx.ehs.web.constants.GlobalConstants;
import ae.pcfc.etks.online.itworx.ehs.web.exception.SystemException;

public class MailUtility {
	
	private JavaMailSender mailSender;
	private SimpleMailMessage templateMessage;
	private SimpleMailMessage fnpTemplateMessage;
	private String bankAccountName;
	private String bankAccountNumber;
	private String bankIBN;
	private String bankSwiftCode;
	private String bankName;
	private SimpleMailMessage documentTemplateMessage;
	private SimpleMailMessage receiptTemplateMessage;
	private String documentAdminName;
	private String financeAdminName;

	public void setDocumentAdminName(String documentAdminName) {
		this.documentAdminName = documentAdminName;
	}

	public void setFinanceAdminName(String financeAdminName) {
		this.financeAdminName = financeAdminName;
	}

	public void setDocumentTemplateMessage(SimpleMailMessage documentTemplateMessage) {
		this.documentTemplateMessage = documentTemplateMessage;
	}

	public void setReceiptTemplateMessage(SimpleMailMessage receiptTemplateMessage) {
		this.receiptTemplateMessage = receiptTemplateMessage;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setTemplateMessage(SimpleMailMessage templateMessage) {
		this.templateMessage = templateMessage;
	}
	
	public void setFnpTemplateMessage(SimpleMailMessage fnpTemplateMessage) {
		this.fnpTemplateMessage = fnpTemplateMessage;
	}

	public void setBankAccountName(String bankAccountName) {
		this.bankAccountName = bankAccountName;
	}

	public void setBankAccountNumber(String bankAccountNumber) {
		this.bankAccountNumber = bankAccountNumber;
	}

	public void setBankName(String bankName) {
		this.bankName = bankName;
	}
	
	public void setBankIBN(String bankIBN) {
		this.bankIBN = bankIBN;
	}

	public void setBankSwiftCode(String bankSwiftCode) {
		this.bankSwiftCode = bankSwiftCode;
	}

	public void sendMail(String toAddress,Map<String, String> parametrsList,File attachFile, String templateName) throws SystemException 
	{
		MimeMessageHelper helper = null;
		MimeMessage message = mailSender.createMimeMessage();

		try {
			if (attachFile != null) {

				helper = new MimeMessageHelper(message, true, "UTF-8");
			} else {
				helper = new MimeMessageHelper(message, false, "UTF-8");
			}

			helper.setValidateAddresses(true);
			helper.setFrom(templateMessage.getFrom());
			if (!GenericValidator.isBlankOrNull(toAddress)) {
				helper.setTo(toAddress);
			} 
			if(templateName.equals(GlobalConstants.FNP_TEMPLATE)){
				helper.setSubject(String.format(fnpTemplateMessage.getSubject()
						,parametrsList.get(GlobalConstants.PAYMENT_ADVICE_NUMBER)
						,parametrsList.get(GlobalConstants.CUSTOMER_COMPANY)));
				helper.setText(String.format(fnpTemplateMessage.getText()
						,parametrsList.get(GlobalConstants.USERNAME)
						,parametrsList.get(GlobalConstants.CUSTOMER_COMPANY)
						,parametrsList.get(GlobalConstants.REQUEST_TYPE)
						,parametrsList.get(GlobalConstants.CREATION_DATE)
						,parametrsList.get(GlobalConstants.SERVICE_FRIENDLY_NAME)
						,parametrsList.get(GlobalConstants.PAYMENT_ADVICE_NUMBER)
						,parametrsList.get(GlobalConstants.PAYMENT_FEES)
						,bankName,bankAccountName,bankAccountNumber,bankIBN,bankSwiftCode),true);
			}else if(templateName.equals(GlobalConstants.DOCUMENT_TEMPLATE)){
				helper.setTo(documentTemplateMessage.getTo());
				helper.setSubject(String.format(documentTemplateMessage.getSubject()
						,parametrsList.get(GlobalConstants.CUSTOMER_COMPANY)));
				helper.setText(String.format(documentTemplateMessage.getText(), 
						documentAdminName, 
						parametrsList.get(GlobalConstants.CUSTOMER_NAME), 
						parametrsList.get(GlobalConstants.CUSTOMER_COMPANY), 
						parametrsList.get(GlobalConstants.DOCUMENT_URL)),true);
			}else if(templateName.equals(GlobalConstants.RECEIPT_TEMPLATE)){
				helper.setTo(receiptTemplateMessage.getTo());
				helper.setSubject(String.format(receiptTemplateMessage.getSubject(),
						parametrsList.get(GlobalConstants.ADVICE_NUMBER)
						,parametrsList.get(GlobalConstants.CUSTOMER_COMPANY)));
				helper.setText(String.format(receiptTemplateMessage.getText(), 
						financeAdminName, parametrsList.get(GlobalConstants.CUSTOMER_NAME), 
						parametrsList.get(GlobalConstants.CUSTOMER_COMPANY), 
						parametrsList.get(GlobalConstants.TRAKHEES_ID),
						parametrsList.get(GlobalConstants.REQUEST_NO), 
						parametrsList.get(GlobalConstants.ADVICE_NUMBER)),true);
			}
			
			
			if (attachFile != null) {
					helper.addAttachment(attachFile.getName(), attachFile);
			}
			mailSender.send(message);
		} catch (MailException ex) {
			throw new SystemException(ex);
		} catch (MessagingException e) {
			throw new SystemException(e);
		}
	}

	public void sendMail(String toAddress, String subject, String body, File attachFile) throws SystemException {

		MimeMessageHelper helper = null;
		MimeMessage message = mailSender.createMimeMessage();

		try {
			if (attachFile != null) {

				helper = new MimeMessageHelper(message, true, "UTF-8");
			} else {
				helper = new MimeMessageHelper(message, false, "UTF-8");
			}

			helper.setValidateAddresses(true);
			helper.setFrom(templateMessage.getFrom());
			if (!GenericValidator.isBlankOrNull(toAddress)) {
				helper.setTo(toAddress);
			} else {
				helper.setTo(templateMessage.getTo());
			}
			if (!GenericValidator.isBlankOrNull(subject)) {
				helper.setSubject(subject);
			} else {
				helper.setSubject(templateMessage.getSubject());
			}
			if (!GenericValidator.isBlankOrNull(body)) {
				helper.setText(body);
			} else {
				helper.setText(templateMessage.getText());
			}
			if (attachFile != null) {
					helper.addAttachment(attachFile.getName(), attachFile);
			}
			mailSender.send(message);
		} catch (MailException ex) {
			throw new SystemException(ex);
		} catch (MessagingException e) {
			throw new SystemException(e);
		}

	}

}
